package com.singzi.leetcode.jianzhiOffer;


import com.singzi.leetcode.obj.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class T0026 {
}
class Solution0026 {
    public boolean isSubStructure(TreeNode A, TreeNode B) {
        if(A == null || B == null) return false;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(A);
        while (!queue.isEmpty()){
            TreeNode treeNode = queue.remove();
            if(isSameTreeNode(treeNode, B)) return true;
            else {
                if(treeNode.left != null) queue.add(treeNode.left);
                if(treeNode.right != null) queue.add(treeNode.right);
            }
        }
        return false;

    }
    public boolean isSameTreeNode(TreeNode A, TreeNode B){
        if(B == null) return true;
        if(A == null) return false;
        if(A.val != B.val) return false;
        return isSameTreeNode(A.left, B.left) && isSameTreeNode(A.right, B.right);

    }
}